package com.linkage.educloud.ah.adapter.pic;

import android.app.Activity;
import android.graphics.Bitmap;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

import com.linkage.educloud.ah.Constants;
import com.linkage.educloud.ah.R;
import com.linkage.educloud.ah.bean.PicItem;
import com.linkage.educloud.ah.view.activity.big_picture.BigPictureActivity;
import com.linkage.educloud.ah.widget.ChoosePicDialog;
import com.zzhou.common.library.util.ImageCompress;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Mr'Tang on 2018/10/11.
 */

public class ChooseImgAdapter extends android.widget.BaseAdapter {

    List<Uri> mList = new ArrayList<>();
    Activity mContext;
    ChoosePicDialog dialog;

    public ChooseImgAdapter(Activity context, List<Uri> list) {
        this.mList = list;
        this.mContext = context;
    }

    @Override
    public int getCount() {
        if (mList.size() == 0) {
            return 1;
        } else {
            return mList.size() < 9 ? mList.size() + 1 : 9;
        }
    }

    @Override
    public Uri getItem(int position) {
        if (getCount() == 1) {
            return null;
        } else {
            return mList.get(position);
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final Holder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext)
                    .inflate(R.layout.item_choose_img,
                            parent, false);
            holder = new Holder();
            holder.item_chooseimg_img = (ImageView) convertView
                    .findViewById(R.id.item_chooseimg_img);
            holder.item_chooseimg_delete = (ImageView) convertView
                    .findViewById(R.id.item_chooseimg_delete);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }

        if (position == mList.size()) {
            holder.item_chooseimg_delete.setVisibility(View.GONE);
            holder.item_chooseimg_img.setImageResource(R.mipmap.item_add_img);
        } else {
            holder.item_chooseimg_delete.setVisibility(View.VISIBLE);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    String img_path = ImageCompress.getRealPathFromUri(mContext, mList.get(position));
                    Bitmap bm = ImageCompress.getBitmapFromFile(img_path);
                    holder.item_chooseimg_img.setImageBitmap(bm);
                }
            });
        }

        holder.item_chooseimg_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mList.remove(position);
                notifyDataSetChanged();
            }
        });

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (position == mList.size()) {
                    dialog = new ChoosePicDialog(mContext);
                    dialog.setShotListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dialog.dismiss();
                            mContext.startActivityForResult(ChoosePicDialog.gotoShot("xiaoma.jpg"), Constants.PIC.PIC_Shot);
                        }
                    });
                    dialog.setAlbumListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dialog.dismiss();
                            mContext.startActivityForResult(ChoosePicDialog.gotoAlbum(), Constants.PIC.PIC_ALBUM);
                        }
                    });
                    dialog.show();
                } else {
                    ArrayList<PicItem> picItems = new ArrayList<>();
                    for (int a = 0; a < mList.size(); a++) {
                        PicItem picItem = new PicItem(ImageCompress.getRealPathFromUri(mContext, mList.get(a)), a);
                        picItems.add(picItem);
                    }

                    BigPictureActivity.launch(mContext, picItems, position);
                }
            }
        });

        return convertView;
    }

    class Holder {
        ImageView item_chooseimg_img;
        ImageView item_chooseimg_delete;
    }
}
